package java03;

import java.util.Arrays;

public class Doublesort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 7, 2, 9, 8, 1, 4};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
    public static void quickSort(int[] arr, int left, int right) {
        if(left > right){
            return;
        }
        int base = arr[left];
        int i = left;
        int j = right;
        while (i != j) {
            while (i < j && arr[j] >= base) {
                j--;
            }
            while (i < j && arr[i] <= base) {
                i++;
            }
            if (i < j) {
                swap(arr, i, j);
            }
        }
        arr[left]=arr[i];
        arr[i]=base;
        quickSort(arr, left, i - 1);
        quickSort(arr, j + 1, right);
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
